#coding:utf-8
'''
* coder  : Dzlua
* email  : 505544956@qq.com
* module : api-frame
* path   : app/apis
* file   : register.py
* time   : 2017/10/21
'''
#--------------------#
from app import mgr
#----------#
from app.models.user import User
#--------------------#

#--------------------#
api = mgr.get_api_of_app()
#--------------------#

#--------------------#
class Register(mgr.ApiBase):
    def __init__(self):
        self.add_arg('name', type=str, required=True,
            help = 'need param name.')
        self.add_arg('password', type=str, required=True,
            help = 'need param password.')
    def post(self):
        name = self.arg('name')
        if User.query.filter_by(name=name).first() is not None:
            return {'message': 'user ' + name + ' exist.'}
        user = User(name=name)
        user.hash_password(self.arg('password'))
        mgr.db.session.add(user)
        mgr.db.session.commit()
        return {'message': 'add user ' + name + ' success.'}
#--------------------#